package com.lsfb.signln.Location.LocationSearch.ReplenishSignIn;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@ContentView(R.layout.aty_hyx_replenish_reason)
public class ReplenishReasonActivity extends LsfbActivity implements ReplenishReasonAdapter.ItemClickListener {
    /**
     * 原因列表
     */
    @ViewInject(R.id.rv_reason)
    private RecyclerView rv_reason;
    private List<ReasonBean.ListBean> reasons;
    private ReplenishReasonAdapter adapter;

    /**
     * 返回Activity的请求码
     */
    private int requestCode = 0;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "选择补录原因");

        reasons = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            reasons.add(String.valueOf(i));
//        }
        adapter = new ReplenishReasonAdapter(this, reasons, R.layout.item_hyx_reasons, this);
        rv_reason.setLayoutManager(new LinearLayoutManager(this));
        rv_reason.setAdapter(adapter);

        getReason();

    }

    /**
     * 获取原因列表数据
     */
    private void getReason() {
        HashMap hashMap = new HashMap();
        new BaseInternet().getData_Get(URLString.GETREPREASON, hashMap, ReasonBean.class, new SearchReasonEvent<ReasonBean>(), true, false);
    }

    @EventAnnotation
    public void eventResult(SearchReasonEvent<ReasonBean> event) {
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList().size() != 0) {
                    reasons.clear();
                    reasons.addAll(event.getData().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    reasons.clear();
                    adapter.notifyDataSetChanged();
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    /**
     * 原因列表点击事件
     *
     * @param model
     */
    @Override
    public void onClick(ReasonBean.ListBean model) {
        Intent intent = new Intent();
        intent.putExtra("datas", model);
        setResult(requestCode, intent);
        finish();
    }
}
